package com.example.administrator.taxicar.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.administrator.taxicar.R;
import com.example.administrator.taxicar.ui.activity.AdressActivity;
import com.example.administrator.taxicar.utils.Constants;
import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.widget.NormalDialog;

import cn.pedant.SweetAlert.SweetAlertDialog;

public class AdressFragment extends Fragment implements View.OnClickListener {

    private TextView start_text, end_text,time_text;
    private RelativeLayout start_layout, end_layout;
    private boolean isFirst = true;
    private Button taxi_button,time_button;
    private LinearLayout xinxi_layout,chuzu_layout,zhuan_layout,shunfeng_layout,time_layout,fujia_layout;
    private ImageView chuzu_button,zhuan_button,shunfeng_button;
    private int recLen = 120;
private View fenge_view;

    public static AdressFragment newInstance() {
        AdressFragment fragment = new AdressFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_adress_layout, container, false);
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        inint();
    }

    private void inint() {
        start_text = (TextView) getView().findViewById(R.id.shangche_text);
        end_text = (TextView) getView().findViewById(R.id.mudi_text);
        start_layout = (RelativeLayout) getView().findViewById(R.id.start_layout);
        start_layout.setOnClickListener(this);
        end_layout = (RelativeLayout) getView().findViewById(R.id.end_layout);
        end_layout.setOnClickListener(this);

        taxi_button= (Button) getView().findViewById(R.id.taxi_button);
        taxi_button.setOnClickListener(this);

        xinxi_layout= (LinearLayout) getView().findViewById(R.id.xinxi_layout);

        chuzu_button= (ImageView) getView().findViewById(R.id.chuzu_button);
        chuzu_button.setOnClickListener(this);
        zhuan_button= (ImageView) getView().findViewById(R.id.zhuan_button);
        zhuan_button.setOnClickListener(this);
        shunfeng_button= (ImageView) getView().findViewById(R.id.shunfeng_button);
        shunfeng_button.setOnClickListener(this);

        chuzu_layout= (LinearLayout) getView().findViewById(R.id.chuzu_layout);
        chuzu_layout.setOnClickListener(this);
        zhuan_layout= (LinearLayout) getView().findViewById(R.id.zhuan_layout);
        zhuan_layout.setOnClickListener(this);
        shunfeng_layout= (LinearLayout) getView().findViewById(R.id.shunfeng_layout);
        shunfeng_layout.setOnClickListener(this);

        time_layout= (LinearLayout) getView().findViewById(R.id.time_layout);

        time_button= (Button) getView().findViewById(R.id.time_button);
        time_button.setOnClickListener(this);

        time_text= (TextView) getView().findViewById(R.id.time_text);

        fenge_view=getView().findViewById(R.id.fenge_view);

        fujia_layout= (LinearLayout) getView().findViewById(R.id.fujia_layout);
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.e("tag","onResume:isFirst："+isFirst);
        if(isFirst){
            SharedPreferences sharedPreferences = getActivity().getSharedPreferences(Constants.ADRESS, Context.MODE_PRIVATE);
            String adress = sharedPreferences.getString(Constants.ADRESS_STR, "");
            Log.e("tag", "adress:" + adress);
            if (!adress.equals("")) {
                start_text.setText(adress);
            }
            isFirst = false;
        }else {
            SharedPreferences sp = getActivity().getSharedPreferences(Constants.ADRESS_SHURU,Context.MODE_PRIVATE);
            String shuru_adress=sp.getString(Constants.ADRESS_SELECTOR,"");
            String end_adress=sp.getString(Constants.ADRESS_END,"");
            if(!shuru_adress.equals("")){
                start_text.setText(shuru_adress);
            }
            if(!end_adress.equals("")){
                end_text.setText(end_adress);
            }
        }

        if(!start_text.getText().toString().equals("从哪儿上车")&&!end_text.getText().toString().equals("要去哪儿")){
            xinxi_layout.setVisibility(View.VISIBLE);
            taxi_button.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start_layout:
                Intent intent = new Intent(getActivity(), AdressActivity.class);
                intent.putExtra(Constants.START,"从哪儿上车");
                startActivity(intent);
                break;
            case R.id.end_layout:
                Intent intent2 = new Intent(getActivity(), AdressActivity.class);
                intent2.putExtra(Constants.START,"要去哪儿");
                startActivity(intent2);
                break;
            case R.id.taxi_button:
                final SweetAlertDialog pDialog = new SweetAlertDialog(getActivity(), SweetAlertDialog.PROGRESS_TYPE)
                        .setTitleText("发单中...");
                pDialog.show();
                pDialog.setCancelable(false);
                new CountDownTimer(800 * 5, 800) {
                    public void onTick(long millisUntilFinished) {
                        pDialog.getProgressHelper().setBarColor(Color.parseColor("#0392ff"));
                    }
                    public void onFinish() {
                        pDialog.dismiss();
                        //发送广播给MainActivity
//                        Intent intent = new Intent(Constants.ADRESSFRAGMENT_BRODCAST);
//                        LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(intent);

                        start_layout.setVisibility(View.GONE);
                        end_layout.setVisibility(View.GONE);
                        xinxi_layout.setVisibility(View.GONE);
                        taxi_button.setVisibility(View.GONE);
                        fenge_view.setVisibility(View.GONE);
                        time_layout.setVisibility(View.VISIBLE);
                        fujia_layout.setVisibility(View.VISIBLE);

                        chuzu_layout.setClickable(false);
                        zhuan_layout.setClickable(false);
                        shunfeng_layout.setClickable(false);

                        Message message = handler.obtainMessage(1);     // Message
                        handler.sendMessageDelayed(message, 1000);
                    }
                }.start();
                break;
            case R.id.chuzu_layout:
                Log.e("tag","chuzu_button");
                clearChecked();
                chuzu_button.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.car_ic_carpool_message));
                taxi_button.setText("呼叫出租车");
                break;
            case R.id.zhuan_layout:
                Log.e("tag","zhuan_button");
                clearChecked();
                zhuan_button.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.car_ic_carpool_message));
                taxi_button.setText("呼叫专车");
                break;
            case R.id.shunfeng_layout:
                Log.e("tag","shunfeng_button");
                clearChecked();
                shunfeng_button.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.car_ic_carpool_message));
                taxi_button.setText("预约顺风车");
                break;
            case R.id.time_button:
                NormalDialogStyleTwo();
                break;

        }
    }

    public void clearChecked(){
        chuzu_button.setImageDrawable(ContextCompat.getDrawable(getActivity(),R.drawable.car_trip_friend_bubble_endpoint));
        shunfeng_button.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.car_trip_friend_bubble_endpoint));
        zhuan_button.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.car_trip_friend_bubble_endpoint));
    }

    private void NormalDialogStyleTwo() {
        final NormalDialog dialog = new NormalDialog(getActivity());
        dialog.isTitleShow(false)
                .content("是否取消订单")//
                .style(NormalDialog.STYLE_TWO)//
                .titleTextSize(23)//
                .show();

        dialog.setOnBtnClickL(
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        //取消
                        dialog.dismiss();
                    }
                },
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                       //确定
                        dialog.dismiss();

                        start_layout.setVisibility(View.VISIBLE);
                        end_layout.setVisibility(View.VISIBLE);
                        xinxi_layout.setVisibility(View.VISIBLE);
                        taxi_button.setVisibility(View.VISIBLE);
                        fenge_view.setVisibility(View.VISIBLE);
                        time_layout.setVisibility(View.GONE);
                        fujia_layout.setVisibility(View.GONE);

                        chuzu_layout.setClickable(true);
                        zhuan_layout.setClickable(true);
                        shunfeng_layout.setClickable(true);

                        recLen=120;
                        handler.removeMessages(1);
                    }
                });
    }

    final Handler handler = new Handler(){

        public void handleMessage(Message msg){         // handle message
            switch (msg.what) {
                case 1:
                    recLen--;
                    time_text.setText("" + recLen);
                    if(recLen > 0){
                        Message message = handler.obtainMessage(1);
                        handler.sendMessageDelayed(message, 1000);      // send message
                    }else{
                        time_text.setVisibility(View.GONE);
                    }
            }
            super.handleMessage(msg);
        }
    };

}
